﻿Imports NUnit.Framework
Imports ValidationFramework
Namespace BusinessBaseClasses
    Public Class DataErrorInfoValidatableBaseLambdaExample
#Region "Example"
        Public Class Person
            Inherits DataErrorInfoValidatableBase
#Region "Fields"
            Private _firstName As String
            Private _lastName As String
            Private _emailAddress As String
#End Region
#Region "Constructor"
            Public Sub New()
                MyBase.New()
                PropertyValidationManager.ValidateAll()
            End Sub
#End Region
#Region "Properties"
            <RequiredStringRule(), LengthStringRule(4)> _
            Public Property FirstName() As String
                Get
                    Return _firstName
                End Get
                Set(ByVal value As String)
                    If _firstName <> value Then
                        _firstName = value
                        ValidateProperty(Function() FirstName)
                    End If
                End Set
            End Property
            <RequiredStringRule(), LengthStringRule(50, Minimum:=2)> _
            Public Property LastName() As String
                Get
                    Return _lastName
                End Get
                Set(ByVal value As String)
                    If _lastName <> value Then
                        _lastName = value
                        ValidateProperty(Function() LastName)
                    End If
                End Set
            End Property
            <RequiredStringRule(), RegexRule("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage:="Invalid email format."), LengthStringRule(50)> _
            Public Property EmailAddress() As String
                Get
                    Return _emailAddress
                End Get
                Set(ByVal value As String)
                    If _emailAddress <> value Then
                        _emailAddress = value
                        ValidateProperty(Function() EmailAddress)
                    End If
                End Set
            End Property
#End Region
        End Class
        <TestFixture()> _
        Public Class ExampleUsage
            <Test()> _
            Public Sub RunIsValid()
                Dim _person = New Person()
                ' Initially not valid
                Assert.IsFalse(_person.IsValid)
                ' Set FirstName, LastName and EmailAddress
                _person.FirstName = "John"
                _person.LastName = "Smith"
                _person.EmailAddress = "John.Smith@email.com"
                ' It is now valid
                Assert.IsTrue(_person.IsValid)
            End Sub
            <Test()> _
            Public Sub RunValidatorResultsInError()
                Dim _person = New Person()
                ' Initially there should be 3 items in ValidatorResultsInError as only 3 
                ' properties have required rules.
                Assert.AreEqual(3, _person.ValidatorResultsInError.Count)

                ' Set LastName to a value and count goes down to 2
                _person.LastName = "Smith"
                ' No need to call IsValid (to force a validation) because properties are 
                ' validated during each set
                Assert.AreEqual(2, _person.ValidatorResultsInError.Count)
            End Sub
            <Test()> _
            Public Sub RunSpecificProperty()
                Dim _person As New Person()

                ' LastName initially has an ErrorMessage because it is invalid
                Dim lastNameErrorMessage As String = _person.GetError(Function() _person.LastName)
                Assert.IsNotNull(lastNameErrorMessage)

                ' Set LastName and it will then have no ErrorMessage
                _person.LastName = "Smith"
                ' No need to call IsValid (to force a validation) because properties are 
                ' validated during each set
                lastNameErrorMessage = _person.GetError(Function() _person.LastName)
                Assert.IsEmpty(lastNameErrorMessage)
            End Sub
        End Class
#End Region
    End Class
End Namespace